package org.rcloud.io.controller;

import com.alibaba.fastjson.JSONObject;
import org.rcloud.io.Utils.PageVo;
import org.rcloud.io.Utils.ResultVo;
import org.rcloud.io.model.Travelapply;
import org.rcloud.io.service.TravelapplyService;
import org.rcloud.io.service.WxPushServiceQcl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * Created by Administrator on 2019/3/21.
 */
@RestController
@RequestMapping(value = "/Travelapply")
public class TravelapplyController {
    @Autowired
    private TravelapplyService travelapplyService;

    /**
     * 旅游报名
     * @return
     */
    @RequestMapping(value = "/add",produces = {"application/json;charset=UTF-8"})
    public int add(@RequestBody String params){
        Travelapply travelapply2 = JSONObject.parseObject(params,Travelapply.class);
        Travelapply travelapply3 = (Travelapply) travelapplyService.selecttimes(travelapply2).getInfo();
        Travelapply travelapply;
        if (null!=travelapply3){
            //当前时间减去7天换算成毫秒数
            Calendar c = new GregorianCalendar();
            Date date = new Date();
            Long is = date.getTime();
            Long ss = System.currentTimeMillis() ;
            Long day7 = 24L*60*60*1000*7;
            Long nowTime = is-day7;// 当前时间减去七天时间的毫秒数
            System.out.println("666666666:"+nowTime);
            Long createtime = travelapply3.getCreatetime().getTime();
            System.out.println("777777777:"+createtime);
            if (nowTime<=createtime){
                System.out.println("=======已报名，距上次报名。。小于等于七天。。不能再次报名！=======");
                return 2;
            }else {
                JSONObject jsonObject = JSONObject.parseObject(params);
                jsonObject.put("id", UUID.randomUUID());
                jsonObject.put("createtime",new Date());
                jsonObject.put("flag",1);
                travelapply = JSONObject.parseObject(jsonObject.toString(), Travelapply.class);
                System.out.println("=======已报名，距上次报名。。大于七天。。再次报名！=======");
            }
        }else {
            System.out.println("=======第一次报名！=======");
            JSONObject jsonObject = JSONObject.parseObject(params);
            jsonObject.put("id", UUID.randomUUID());
            jsonObject.put("createtime",new Date());
            jsonObject.put("flag",1);
            travelapply = JSONObject.parseObject(jsonObject.toString(), Travelapply.class);
        }
        // 如果报名成功就发送模板消息
        if (Integer.valueOf(travelapplyService.insertSelective(travelapply).getInfo().toString())==1){
            WxPushServiceQcl wxPushServiceQcl = new WxPushServiceQcl();
            wxPushServiceQcl.pushOneUser2(travelapply.getUserid(),travelapply.getFromid(),
                    travelapply.getLinename(),travelapply.getName(),travelapply.getTelephone(),
                    travelapply.getPeoplenum(),travelapply.getCreatetime());
            System.out.println("=======报名成功，发送消息模板！=======");
            return 1;
        }else {
            System.out.println("=======报名失败！=======");
            return 0;
        }
    }

    /**
     * 报名统计（全部）
     * @return
     */

    @RequestMapping(value = "/selectAll",produces = {"application/json;charset=UTF-8"})
    public PageVo<Travelapply> selectAll(){
        return travelapplyService.selectAll();
    }

    /**
     * 报名统计（线路）
     * @return
     */

    @RequestMapping(value = "/select",produces = {"application/json;charset=UTF-8"})
    public PageVo<Travelapply> select(Travelapply params){
        return travelapplyService.selectByPrimaryKey(params);
    }



    @RequestMapping(value = "/selectnum",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    @CrossOrigin
    public ResultVo selectNum(){
        return travelapplyService.selectNum();
    }
}
